<script lang="ts" name="goods" setup>
import Message from "@/components/message";
import useStore from "@/store";
import { ref, watchEffect } from "vue";
import { storeToRefs } from "pinia";
import { useRoute } from "vue-router";
import {CartItem} from '@/types/cart'
import GoodsImage from "./components/goods-image.vue";
import GoodsSale from "./components/goods-sale.vue";
import GoodsInfo from "./components/goods-info.vue";
import GoodsSku from "./components/goods-sku.vue";
import GoodsDetail from "./components/goods-detail.vue";
import GoodsHot from "./components/goods-hot.vue";

const route = useRoute();
const { goods, cart } = useStore();
const { info } = storeToRefs(goods);
watchEffect(() => {
  const id = route.params.id as string;
  if (route.fullPath === `/goods/${id}`) {
    goods.getGoodsInfo(id);
  }
});
const count = ref(1);

const changeSku = (skuId: string) => {
  currentSkuId.value = skuId;
  const sku = info.value.skus.find((item) => item.id === skuId);
  if (sku) {
    info.value.inventory = sku.inventory;
    info.value.price = sku.price;
    info.value.oldPrice = sku.oldPrice;
  }
};

const currentSkuId = ref("");
// 加购
const addCart = async () => {
  if (!currentSkuId.value) {
    return Message.warning("请选择完整信息");
  }
  const sku = info.value.skus.find((item) => item.id === currentSkuId.value)!;
  const attrsText = sku.specs
    .map((item) => item.name + ":" + item.valueName)
    .join(" ");
  await cart.addCart({
    skuId: currentSkuId.value,
    count: count.value,
    id: info.value.id,
    name: info.value.name,
    picture: info.value.mainPictures[0],
    price: info.value.price,
    attrsText,
    selected: true,
    nowPrice: info.value.price,
    stock: info.value.inventory,
    isEffective: true,
  } as CartItem);
  Message.success("加入购物车成功");
};
</script>

<template>
  <div class="xtx-goods-page">
    <div class="container" v-if="info.categories">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem to="/">{{ info.categories[0].name }}</XtxBreadItem>
        <XtxBreadItem to="/">{{ info.categories[1].name }}</XtxBreadItem>
        <XtxBreadItem to="/">{{ info.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <GoodsImage :images="info.mainPictures" />
          <GoodsSale :goods="info" />
        </div>
        <div class="spec">
          <GoodsInfo :goods="info" />
          <!-- 商品规格 -->
          <GoodsSku
            :goods="info"
            @changeSku="changeSku"
            :key="Math.random()"
            skuId="1369155864430120962"
          />
          <!-- 数字选择框 -->
          <XtxNumbox v-model="count" />
          <!-- 按钮 -->
          <XtxButton
            style="margin-top: 20px"
            type="primary"
            size="middle"
            @click="addCart"
            >加入购物车</XtxButton
          >
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <!-- 商品详情 -->
            <GoodsDetail :goods="info" />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1" />
          <GoodsHot :type="2" />
          <GoodsHot :type="3" />
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
</style>
